package com.shqcjd.sinopecdemomobile.ui.activity;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.ui.base.BaseActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.qrcode.core.BGAQRCodeUtil;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

/**
 * 生成二维码
 */
public class GenerateQRcodeActivity extends BaseActivity {


    @BindView(R.id.iv_toolbar_back)
    ImageView ivToolbarBack;
    @BindView(R.id.tv_toolbar_center)
    TextView tvToolbarCenter;
    @BindView(R.id.iv_qr_code)
    ImageView ivQrCode;
    private String qrcode;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_generate_qrcode;
    }

    @Override
    public void initView() {
        tvToolbarCenter.setText("生成二维码");
        qrcode = getIntent().getStringExtra("QRcode");
        Logger.i("巡检："+qrcode);
        generateCode();
    }

    private void generateCode() {
        new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                return QRCodeEncoder.syncEncodeQRCode(qrcode, BGAQRCodeUtil.dp2px(GenerateQRcodeActivity.this, 150));
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if (bitmap != null) {
                    ivQrCode.setImageBitmap(bitmap);
                } else {
                    Toast.makeText(GenerateQRcodeActivity.this, "生成二维码失败", Toast.LENGTH_SHORT).show();
                }
            }
        }.execute();
    }


    @OnClick(R.id.iv_toolbar_back)
    public void onViewClicked() {
        finish();
    }
}
